<template>
  <body id="paper">
  <Header></Header>
  <el-form :model="registerForm" status-icon :rules="rules" class="register-container" label-position="left"
           label-width="0px" v-loading="loading">
    <h3 class="register_title">用户注册</h3>
    <el-form-item prop="acc">
      <el-input type="text" v-model="registerForm.acc"
                auto-complete="off" placeholder="账号"></el-input>
    </el-form-item>
    <el-form-item prop="pw">
      <el-input type="password" v-model="registerForm.pw"
                auto-complete="off" placeholder="密码"></el-input>
    </el-form-item>
    <el-form-item prop="cpw">
      <el-input type="password" v-model="registerForm.cpw"
                autocomplete="off" placeholder="确认密码"></el-input>
    </el-form-item>
    <el-form-item prop="sfid">
      <el-input type="text" v-model="registerForm.sfid"
                auto-complete="off" placeholder="身份id"></el-input>
    </el-form-item>
    <el-form-item style="width: 100%">
      <el-button type="primary" style="width: 40%;background: #9495c5;border: none" v-on:click="register">注册</el-button>
      <router-link to="login">
        <el-button type="primary" style="width: 40%;background: #9495c5;border: none">返回</el-button>
      </router-link>
    </el-form-item>
  </el-form>
  </body>
</template>
<script>
import Header from './common/Header'

export default {
  components: {Header},
  data () {
    var checkSFID = (rule, value, callback) => {
      if (value === '') {
        return callback(new Error('请输入身份ID'))
      }
      setTimeout(() => {
        // 后期加上正则表达式判断接收的字符串是否仅包含数字
        if (!Number.isInteger(value)) {
          callback(new Error('输入的身份ID格式错误'))
        }
        // 后期加上id的格式识别
        // else {
        //   if (value > 2000) {
        //     callback(new Error(''))
        //   } else {
        //     callback()
        //   }
        // }
      }, 1000)
    }
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      }
    }
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.registerForm.pw) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }

    return {
      checked: true,
      registerForm: {
        acc: '',
        pw: '',
        cpw: '',
        sfid: ''
      },
      rules: {
        acc: [{required: true, message: '用户名不能为空', trigger: 'blur'}],
        pw: [{validator: validatePass, trigger: 'blur'}],
        cpw: [{validator: validatePass2, trigger: 'blur'}],
        sfid: [{validator: checkSFID, trigger: 'blur'}]
      },
      loading: false
    }
  },
  methods: {
    register () {
      if (this.registerForm.acc === '' ||
        this.registerForm.pw === '' || this.registerForm.cpw === '' || this.registerForm.sfid === '') {
        this.$alert('请填写完整信息', '提示', {
          confirmButtonText: '确定'
        })
      } else {
        if (this.registerForm.pw !== this.registerForm.cpw) {
          this.$alert('两次输入的密码不一致', '提示', {
            confirmButtonText: '确定'
          })
        } else {
          var _this = this
          this.$axios
            .post('/register', {
              acc: this.registerForm.acc,
              pw: this.registerForm.pw,
              sfid: this.registerForm.sfid
            })
            .then(resp => {
              if (resp.data.code === 200) {
                this.$alert('注册成功', '提示', {
                  confirmButtonText: '确定'
                })
                _this.$router.replace('/login')
              } else {
                this.$alert(resp.data.msg, '提示', {
                  confirmButtonText: '确定'
                })
              }
            })
            .catch(failResponse => {
              alert('服务器响应失败！')
            })
        }
      }
    }
  }
}
</script>
<style>
#paper {
  background: #696aad url("../assets/img/bg/bg1.png") no-repeat 20% 40%;
  background-size: 20%;
  height: 100%;
  width: 100%;
  position: fixed;
}

body {
  margin: 0;
}

.register-container {
  border-radius: 15px;
  background-clip: padding-box;
  margin: 90px auto;
  position: fixed;
  top: 20%;
  right: 20%;
  width: 350px;
  padding: 35px 35px 15px 35px;
  background: #fff;
  border: 1px solid #eaeaea;
  box-shadow: 0 0 25px #cac6c6;
}

.register_title {
  margin: 0px auto 40px auto;
  text-align: center;
  color: #9495c5;
}
</style>
